(function () {
    var requestAnimationFrame = (window).requestAnimationFrame || (window).mozRequestAnimationFrame || (window).webkitRequestAnimationFrame || window.msRequestAnimationFrame || (window).oRequestAnimationFrame;
    window.requestAnimationFrame = requestAnimationFrame;

    var cancelAnimationFrame = (window).cancelAnimationFrame || (window).mozCancelAnimationFrame || (window).webkitCancelAnimationFrame || window.msCancelRequestAnimationFrame || (window).oCancelRequestAnimationFrame;
    window.cancelAnimationFrame = cancelAnimationFrame;
})();

var TSUI;
(function (TSUI) {
    (function (Animation) {
        var AnimationCallback = (function () {
            function AnimationCallback(callback, context, single) {
                if (typeof single === "undefined") { single = false; }
                this.callback = callback;
                this.context = context;
                this.single = single;
                this.AnimationStartTime = -1;
            }
            return AnimationCallback;
        })();
        Animation.AnimationCallback = AnimationCallback;

        var AnimationIntervalRef = -1;

        var AnimationCallbacks = new TSUI.Collections.List();

        Animation.RegisterAnimationCallback = function (obj) {
            AnimationCallbacks.Add(obj);
            return obj;
        };

        Animation.RegisterAnimationForSingleCallback = function (obj) {
            obj.single = true;
            AnimationCallbacks.Add(obj);
            return obj;
        };

        Animation.UnregisterAnimationCallback = function (obj) {
            AnimationCallbacks.Remove(obj);
            return obj;
        };

        var AnimationCallbacks_Modified = function (eventArgs) {
            if (AnimationCallbacks.Count() === 0) {
                if (AnimationIntervalRef !== -1) {
                    if (window.requestAnimationFrame) {
                        window.cancelAnimationFrame(AnimationIntervalRef);
                        AnimationIntervalRef = -1;
                    } else {
                        clearInterval(AnimationIntervalRef);
                        AnimationIntervalRef = -1;
                    }
                }
            } else {
                if (AnimationIntervalRef === -1) {
                    if (window.requestAnimationFrame) {
                        AnimationIntervalRef = window.requestAnimationFrame(AnimationIntervaFunc);
                    } else {
                        AnimationIntervalRef = setInterval(AnimationIntervaFunc, 70);
                    }
                }
            }
        };

        var AnimationIntervaFunc = function () {
            var elems = AnimationCallbacks.ToArray();
            var elapsedTime = -1;
            var removeElems = new TSUI.Collections.List();
            var doRemove = false;
            for (var i = 0; i < elems.length; i++) {
                var callback = elems[i];
                if (callback.AnimationStartTime === -1) {
                    callback.AnimationStartTime = Date.now();
                }
                elapsedTime = Date.now() - callback.AnimationStartTime;
                callback.callback.call(elems[i].context, elapsedTime);
                if (callback.single) {
                    removeElems.Add(elems[i]);
                    doRemove = true;
                }
            }
            if (doRemove) {
                AnimationCallbacks.RemoveAll(removeElems);
            }

            if (window.requestAnimationFrame) {
                AnimationIntervalRef = window.requestAnimationFrame(AnimationIntervaFunc);
            } else {
                $("body").css({ zIndex: $("body").css("z-index") === "1" ? 0 : 1 });
            }
        };
        AnimationCallbacks.OnModified.Attach(new TSUI.Events.CollectionModifiedEventHandler(AnimationCallbacks_Modified, AnimationCallbacks));

        var AnimationHelper = (function () {
            function AnimationHelper() {
            }
            AnimationHelper.PrepareHTML2CanvasForAnimation = function (elem, callback) {
                var elems = elem.find("*").not(".Hidden");
                elem.attr("data-html2canvas-visible", "");
                elems.attr("data-html2canvas-visible", "");

                html2canvas([elem[0]], {
                    onrendered: function (canvas) {
                        var newCanvas = $(canvas);

                        newCanvas.css({
                            position: "absolute",
                            top: elem.offset().top,
                            left: elem.offset().left,
                            zIndex: elem.css("z-index"),
                            display: "none"
                        });

                        $("body").append(newCanvas);
                        elem.removeAttr("data-html2canvas-visible");
                        elems.removeAttr("data-html2canvas-visible");

                        callback(newCanvas);
                    }
                });
            };

            AnimationHelper.RestoreAfterAnimationHTML2Canvas = function (elem, canvas) {
                var zIndex = parseInt(canvas.css("z-index"), 10);
                canvas.css({ opacity: 1, zIndex: zIndex });
                elem.css({
                    display: "",
                    opacity: 0,
                    visibility: "visible",
                    zIndex: zIndex + 2
                }).animate({
                    opacity: 1
                }, 500, "swing", function () {
                    elem.css({
                        opacity: 1
                    });
                    canvas.animate({
                        opacity: 0
                    }, 500, "swing", function () {
                        canvas.css("display", "none");
                        canvas.remove();
                        elem.css({
                            zIndex: zIndex
                        });
                    });
                });
            };
            return AnimationHelper;
        })();
        Animation.AnimationHelper = AnimationHelper;
    })(TSUI.Animation || (TSUI.Animation = {}));
    var Animation = TSUI.Animation;
})(TSUI || (TSUI = {}));
